/**
 * Created by xwang on 2018-09-12.
 */
var request = require("superagent");
var API = require("./support/const");
var service = require("../../service/log");


/**
 * 检查项
 * **/
exports.inspect = {

    all: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };


        request.get(API.records.inspect)
            .set(header)
            //.query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取检查项列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
};
/**
 * 作业
 * **/
exports.records = {
    list: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };

        let params = req.body;
        request.get(API.records.list)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取检查作业列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    detail: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.get(API.records.detail + id)
            .set(header)
            .query({id: id})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '获取检查作业详情'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    update: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let params= req.body;
        request.put(API.records.update3)
            .type('application/json')
            .set(header)
            .send(JSON.stringify(params))
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '保存检查作业','mark':params}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
    submit: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.put(API.records.submit + id)
            .set(header)
            .query({unitId: id})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '提交检查作业'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    reject: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.put(API.records.reject + id)
            .set(header)
            .query({unitId: id})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '打回作业'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    getGenerateBugList: function (req, res, next) {

        let header = {
            token: req.cookies.token
        };
        let {id} = req.body;
        request.get(API.records.generate_bug_list + id)
            .set(header)
            .query({unitId: id})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '资料导出'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    }
};